package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.entity;

import com.cqhilink.api.common.utils.UniqueUtil;
import com.cqhilink.api.common.utils.enums.RespCodeEnum;
import com.cqhilink.iot.jobstandardmanagement.facade.service.regionalmaintenance.RegionalMaintenanceService;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.BackMessage;
import  com.cqhilink.api.common.utils.date.DateUtil;
import com.cqhilink.iot.jobstandardmanagement.facade.utils.TreeIconSkin;
import com.cqhilink.iot.jobstandardmanagement.dao.mapper.*;
import com.cqhilink.iot.jobstandardmanagement.facade.model.*;
import com.cqhilink.iot.jobstandardmanagement.facade.service.OutZtreeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.department.JsmDepartmentService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.device.JsmDeviceService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.entity.JsmEntityService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.entitytype.JsmEntityTypeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.file.JsmFileService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.organization.JsmOrganizationService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.servicenode.JsmServiceNodeService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.station.JsmStationService;
import com.cqhilink.iot.jobstandardmanagement.facade.service.well.JsmWellService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.jobstandardmanagement.services.innerservice.impl.entity
 * @Description: ${TODO}(生产实体服务层实现类)
 * @author: YuPeng
 * @date: 2016/12/2,16:15
 */

public class JsmEntityServiceImpl implements JsmEntityService {

    @Autowired
    private EntityMapper entityMapper;
    @Autowired
    private OrganizationMapper orgMapper;
    @Autowired
    private EntityTypeMapper enTypeMapper;
    @Autowired
    private ManufactorMapper manuMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private JsmDeviceService deviceService;
    @Autowired
    private EntityProMapper entityProMapper;
    @Autowired
    private EntityTypePropertyMapper entityTypePropertyMapper;
    @Autowired
    private JsmFileService fileService;
    @Autowired
    private JsmOrganizationService organizationService;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private JsmServiceNodeService serviceNodeService;
    @Autowired
    private JsmEntityTypeService entityTypeService;
    @Autowired
    private JsmStationService stationService;
    @Autowired
    private JsmWellService wellService;
    @Autowired
    private OutZtreeService outZtreeService;
    @Autowired
    private JsmDepartmentService departmentService;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private EntityPropertyMapMapper entityPropertyMapMapper;
    @Autowired
    private RegionalMaintenanceService regionalMaintenanceService;

    /**
     * 创建实体书顶级树名
     *
     * @return
     */
    private Ztree getTop() {
        Ztree top = new Ztree();
        top.setId("0");
        top.setName("生产实体");
        top.setIsParent(RespCodeEnum.RESP_CODE_ENUM_true.code());
        top.setOrgLevel("-1");
        top.setpId("-1");
        top.setIconSkin(TreeIconSkin.ICON_ORG.getDesc());
        return top;
    }

    /**
     * 获取生产实体树及下级实体
     *
     * @param isOrg 1是组织 id 是组织Id
     * @param id
     * @return
     * @throws Exception modify time , 查询当前人的组织机构 然后
     */
    @Override
    public List<Ztree> getEntityTree(String isOrg, String id, Employee employee) throws Exception {
       /* int isOpenLevel = 0 ; // 控制组织机构显示层级 ， 查询当前人的组织机构 ， 然后查询出当前设备 和 当前设备下级设备
        List<Ztree> ztList = new ArrayList<>();
        List<Organization> orgList = new ArrayList<>();
        //对参数进行判空
        if (StringUtils.isBlank(isOrg) && StringUtils.isBlank(id)) {
            Employee employee =  employeeService.getLoginEmployee();
           // ztList.addAll(getOrgZtreeList(orgMapper.selectOrgByLevel("3")));
            Organization organization = organizationMapper.selectByPrimaryKey(employee.getOrgId());
            orgList.add(organization);
            ztList.addAll(getOrgZtreeList(orgList));
        //如果当前是实体，按上级实体ID，查询下级实体
        } else if (StringUtils.isNotBlank(isOrg) && !"1".equals(isOrg) && !StringUtils.isBlank(id)) {
            ztList.addAll(getEntityZtreeList(entityMapper.selectByPEId(id)));
        //如果当前是机构，按机构ID查询下级的机构及实体并转换为Ztree
        } else if (StringUtils.isNotBlank(isOrg) && "1".equals(isOrg) && !StringUtils.isBlank(id)) {
            ztList.addAll(getOrgZtreeList(orgMapper.selectOrgByOrgPId(id)));
            ztList.addAll(getEntityZtreeList(entityMapper.selectByPOId(id)));
        }


        return ztList;*/

        List<Ztree> ztList = new ArrayList<>();
        try {
            if (StringUtils.isBlank(isOrg) && StringUtils.isBlank(id)) {
                //Employee employee = employeeService.getLoginEmployee();
                Organization organization = organizationService.selectOrgByOrgId(employee.getOrgId());
                List<Organization> organizationList = new ArrayList<>();
                organizationList.add(organization);
                ztList.addAll(getOrgZtreeList(organizationList));
                //ztList.addAll(getOrgZtreeList(orgMapper.selectOrgByLevel("3")));
                //如果当前是实体，按上级实体ID，查询下级实体
            } else if (StringUtils.isNotBlank(isOrg) && !"1".equals(isOrg) && !StringUtils.isBlank(id)) {
                ztList.addAll(getDeviceZtreeList(deviceMapper.selectByPEId(id)));
                //如果当前是机构，按机构ID查询下级的机构及实体并转换为Ztree
            } else if (StringUtils.isNotBlank(isOrg) && "1".equals(isOrg) && !StringUtils.isBlank(id)) {
                ztList.addAll(getOrgZtreeList(orgMapper.selectOrgByOrgPId(id)));
                //ztList.addAll(getDeviceZtreeList(deviceMapper.selectByPOId(id)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ztList;
    }


    /**
     * 将设备转换为树
     *
     * @param dvList
     * @return
     * @throws Exception
     */
    private List<Ztree> getDeviceZtreeList(List<DeviceVO> dvList) throws Exception {
        List<Ztree> tree = new ArrayList<Ztree>();
        // 将设备转成树形
        for (DeviceVO device : dvList) {
            Ztree enTree = new Ztree();
            enTree.setId(device.getDeviceId());
            enTree.setName(device.getDeviceName());
            enTree.setOpen(RespCodeEnum.RESP_CODE_ENUM_false.code());
            enTree.setIsEntity(1);
            enTree.setIconSkin(TreeIconSkin.ICON_ENTITY.getDesc());
            enTree.setIsParent(deviceMapper.selectCountByPid(device.getDeviceId()) > 0 ? RespCodeEnum.RESP_CODE_ENUM_true.code() : RespCodeEnum.RESP_CODE_ENUM_false.code());
            // 设置父级id
            if (StringUtils.isNotEmpty(device.getParentDeviceId())) {
                enTree.setpId(device.getParentDeviceId());
            } else if (StringUtils.isNotEmpty(device.getOrgId())) {
                enTree.setpId(device.getOrgId());
            }
            tree.add(enTree);
        }
        return tree;
    }

    /**
     * 获取带有特性的实体树
     *
     * @param isOrg
     * @param id
     * @return
     * @throws Exception
     */
    public List<Ztree> getEntityTreeWithPro(String isOrg, String id) throws Exception {
        List<Ztree> ztList = new ArrayList<>();
        List<Organization> orgList = new ArrayList<>();
        //对参数进行判空
        if (StringUtils.isBlank(isOrg) && StringUtils.isBlank(id)) {
            ztList.add(getTop());
            ztList.addAll(getOrgZtreeList(orgMapper.selectOrgByLevel("3")));
        } else if (!StringUtils.isBlank(isOrg) && !"1".equals(isOrg) && !StringUtils.isBlank(id)) {
            ztList.addAll(getEntityZtreeList(entityMapper.selectByPEId(id)));
            EntityVO entity = entityMapper.selectByEnId(id);
            //获取设备特性Ztreelist
            ztList.addAll(getDeviceProZtreeList(entity));
        } else if (!StringUtils.isBlank(isOrg) && "1".equals(isOrg) && !StringUtils.isBlank(id)) {
            ztList.addAll(getOrgZtreeList(orgMapper.selectOrgByOrgPId(id)));
            ztList.addAll(getEntityZtreeList(entityMapper.selectByPOId(id)));
        }
        return ztList;
    }

    /**
     * 获取实体绑定的设备特性列表
     *
     * @param entityVO
     * @return
     * @throws Exception
     */
    private List<Ztree> getDeviceProZtreeList(EntityVO entityVO) throws Exception {
        if (StringUtils.isBlank(entityVO.getDeviceId())) {
            return new ArrayList<>();
        }
        //获取设备特性列表
        Map<String, Object> proMap = deviceService.getDevicePro(new Device(entityVO.getDeviceId()));
        List<EntityPro> proList = (List<EntityPro>) proMap.get("rows");
        if (proList == null || proList.size() == 0) {
            return new ArrayList<>();
        }
        List<Ztree> treelist = new ArrayList<Ztree>();
        //遍历设备特性并转换为Ztree
        for (EntityPro pro : proList) {
            Ztree tree = new Ztree();
            tree.setId(pro.getPropertyName());
            tree.setpId(entityVO.getEnId());
            tree.setName(pro.getPropertyName());
            tree.setIconSkin(TreeIconSkin.ICON_DEVICE_CHARA.getDesc());
            tree.setIsParent(RespCodeEnum.RESP_CODE_ENUM_false.code());
            tree.setOpen(RespCodeEnum.RESP_CODE_ENUM_false.code());
            treelist.add(tree);
        }
        return treelist;
    }

    /**
     * 添加实体信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> addEntity(Entity entity, List<EntityPro> entityPros, String uploadFiles, String deletedFiles, Employee employee) throws Exception {
        Map<String, Object> map = isBlank(entity);
        String creatorId = employee.getEmployeeId();
        if ((Boolean) map.get("state") == RespCodeEnum.RESP_CODE_ENUM_false.code()) {
            return map;
        }
        entity.setEnId(UniqueUtil.uuid());
        entity.setCreatorId(creatorId);
        entity.setStatus(0);
        entity.setModifierId(creatorId);
        entity.setModifyTime(DateUtil.getTimeString());
        entity.setCreateTime(DateUtil.getTimeString());


        if (entityMapper.insert(entity) > 0) {
            for (EntityPro entityPro : entityPros) {
               /* if(opcPointMapper.getOpcPointByApId(entityPro.getPropertyOpc())<=0){
                    OpcPoint opcPoint=new OpcPoint();
                    opcPoint.setApId(entityPro.getPropertyOpc());
                    opcPoint.setApName(entityPro.getPropertyName());
                    opcPointMapper.insert(opcPoint);
                }*/
                entityPro.setDevicePropertyId(UniqueUtil.uuid());
                entityPro.setEntityId(entity.getEnId());
                entityPro.setStatus(new BigDecimal(0));
                entityPro.setCreator(creatorId);
                entityPro.setEntityTypeId(entity.getEnTypeId());
                entityPro.setCreateTime(DateUtil.getTimeString());
                entityPro.setModifyer(creatorId);
                entityPro.setModifyTime(DateUtil.getTimeString());
                entityProMapper.insert(entityPro);
            }
            //保存上传图像
            fileService.addFile(uploadFiles, deletedFiles, entity.getEnId());
            map.put("msg", BackMessage.ADD_OK);
            return map;
        }


        map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
        map.put("msg", BackMessage.ADD_FAIL);
        return map;
    }


    /**
     * 按实体ID删除实体信息
     *
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean delEntityById(String enid) throws Exception {
        Entity entity = new Entity();
        entity.setEnId(enid);
        entity.setStatus(1);
        if (entityMapper.update(entity) > 0) return RespCodeEnum.RESP_CODE_ENUM_true.code();
        return RespCodeEnum.RESP_CODE_ENUM_false.code();
    }

    /**
     * 修改实体信息
     *
     * @param entity
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Map<String, Object> modifyEntity(Entity entity, List<EntityPro> entityPros, String uploadFiles, String deletedFiles, Employee employee) throws Exception {

        String modifierId = employee.getEmployeeId();
        Map<String, Object> map = isBlank(entity);
        //校验失败返回信息
        if ((Boolean) map.get("state") == RespCodeEnum.RESP_CODE_ENUM_false.code()) {
            return map;
        }
        entity.setModifierId(modifierId);
        entity.setModifyTime(DateUtil.getTimeString());
        entity.setOperateTime(DateUtil.getTimeString());

        //持久化实体信息
        if (entityMapper.update(entity) > 0) {
            entityProMapper.deleteDeviceProByEntityId(entity.getEnId());
            for (EntityPro entityPro : entityPros) {
             /*   if(opcPointMapper.getOpcPointByApId(entityPro.getPropertyOpc())<=0){
                    OpcPoint opcPoint=new OpcPoint();
                    opcPoint.setApId(entityPro.getPropertyOpc());
                    opcPoint.setApName(entityPro.getPropertyName());
                    opcPointMapper.insert(opcPoint);
                }*/
                entityPro.setDevicePropertyId(UniqueUtil.uuid());
                entityPro.setEntityId(entity.getEnId());
                entityPro.setCreator(modifierId);
                entityPro.setEntityTypeId(entity.getEnTypeId());
                entityPro.setCreateTime(DateUtil.getTimeString());
                entityPro.setModifyer(modifierId);
                entityPro.setModifyTime(DateUtil.getTimeString());
                entityProMapper.insert(entityPro);
            }

            //保存上传图像
            fileService.updateFile(uploadFiles, deletedFiles, entity.getEnId());

            map.put("msg", BackMessage.MODIFY_OK);
            return map;
        }
        map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
        map.put("msg", BackMessage.MODIFY_FAIL);
        return map;
    }

    /**
     * 按实体ID查询实体信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public EntityVO getEntityById(String id) throws Exception {
        EntityVO entity = entityMapper.selectByEnId(id);
        if (entity != null) {
            //非空判断
            formatDate(entity);

            //获取上级机构
            if (!StringUtils.isBlank(entity.getParentOrgId())) {
                Organization org = orgMapper.selectOrgByOrgId(entity.getParentOrgId());
                OrganizationVO orgvo = new OrganizationVO(org.getOrgId(), org.getOrgName());
                entity.setOrg(orgvo);
            }
            //获取生产实体类型
            if (!StringUtils.isBlank(entity.getEnTypeId())) {
                EntityType entype = enTypeMapper.selectByPrimaryKey(entity.getEnTypeId());
                EntityTypeVO entityTypeVO = new EntityTypeVO(entype.getEntityTypeId(), entity.getEnName());
                entity.setEnType(entityTypeVO);
            }
            //获取上级生产实体
            if (!StringUtils.isBlank(entity.getParentEnId())) {
                EntityVO entityVO = entityMapper.selectByEnId(entity.getParentEnId());
                EntityVOSimple entityVOSim = new EntityVOSimple(entityVO.getEnId(), entityVO.getEnName(), entityVO.getEnTypeLevel());
                entity.setEntityVO(entityVOSim);
            }
            //获取生产厂商
            if (!StringUtils.isBlank(entity.getManuId())) {
                ManufactorVO manu = manuMapper.selectById(entity.getManuId());
                entity.setManu(manu);
            }
            //获取设备信息
            if (!StringUtils.isBlank(entity.getDeviceId())) {
                DeviceVO device = deviceMapper.selectByDeviceId(entity.getDeviceId());
                entity.setDevice(device);
            }
        }
        return entity;
    }

    /**
     * 取消日期后面的.0
     *
     * @param entity
     */
    private void formatDate(EntityVO entity) {
        //对创建日期进行格式化
        if (entity.getCreateTime() != null && entity.getCreateTime().length() > 19) {
            entity.setCreateTime(entity.getCreateTime().substring(0, entity.getCreateTime().length() - 2));
        }
        //对修改日期进行格式化
        if (entity.getModifyTime() != null && entity.getModifyTime().length() > 19) {
            entity.setModifyTime(entity.getModifyTime().substring(0, entity.getModifyTime().length() - 2));
        }
        //对运行日期进行格式化
        if (entity.getOperateTime() != null && entity.getOperateTime().length() > 19) {
            entity.setOperateTime(entity.getOperateTime().substring(0, entity.getOperateTime().length() - 2));
        }
    }

    /**
     * 查询生产实体
     *
     * @param entity
     * @return
     */
    @Override
    public List<EntityVO> searchEntity(Entity entity) {
        return entityMapper.selectByEntity(entity);
    }

    /**
     * 获取生产厂商列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<ManufactorVO> getManufactor() throws Exception {
        List<ManufactorVO> manuList = manuMapper.selectAll();
        return manuList;
    }

    /**
     * 按实体ID获取实体详细信息
     *
     * @param entityId
     * @return
     * @throws Exception
     */
    @Override
    public EntityVO findEntity(String entityId) throws Exception {
        return entityMapper.selectByEnId(entityId);
    }

    /**
     * 将组织机构列表转换为树结构
     *
     * @param orgList
     * @return
     * @throws Exception
     */
    private List<Ztree> getOrgZtreeList(List<Organization> orgList) throws Exception {
        List<Ztree> tree = new ArrayList<Ztree>();
        for (Organization org : orgList) {
            Ztree demo = new Ztree();
            demo.setId(org.getOrgId());
            demo.setpId(org.getParentOrgId());
            demo.setName(org.getOrgName());
            demo.setOrgLevel(org.getOrgLevelId());
            demo.setIsOrg(1);
            if ("1".equals(org.getOrgLevelId())) {
                demo.setIconSkin(TreeIconSkin.ICON_BUIDING.getDesc());

            }
            if ("2".equals(org.getOrgLevelId())) {
                demo.setIconSkin(TreeIconSkin.ICON_FLAG.getDesc());
            }
            if ("3".equals(org.getOrgLevelId())) {
                demo.setIconSkin(TreeIconSkin.ICON_OA.getDesc());
                demo.setOpen(RespCodeEnum.RESP_CODE_ENUM_false.code());
            }
            if ("4".equals(org.getOrgLevelId())) {
                demo.setIconSkin(TreeIconSkin.ICON_STATION.getDesc());
                demo.setOpen(RespCodeEnum.RESP_CODE_ENUM_false.code());
            }
            if ("5".equals(org.getOrgLevelId())) {
                demo.setIconSkin(TreeIconSkin.IONC_WELL.getDesc());
                demo.setOpen(RespCodeEnum.RESP_CODE_ENUM_false.code());
            }
            int orgNum = 0;
            if (Integer.parseInt(org.getOrgLevelId()) < 4) {
                orgNum = orgMapper.selectCountByPOId(org.getOrgId());
            }
            //判断是否含有下属生产实体，含有则为true，没有则为RespCodeEnum.RESP_CODE_ENUM_false.code()
            boolean isParent = true; //entityMapper.selectCountByPOId(org.getOrgId()) > 0 || orgNum > 0 ? RespCodeEnum.RESP_CODE_ENUM_true.code() : RespCodeEnum.RESP_CODE_ENUM_false.code();
            demo.setIsParent(isParent);
            tree.add(demo);
        }
        return tree;
    }

    /**
     * 将生产实体列表列表转换为树结构
     *
     * @param enList
     * @return
     * @throws Exception
     */
    private List<Ztree> getEntityZtreeList(List<EntityVO> enList) throws Exception {
        List<Ztree> tree = new ArrayList<Ztree>();
        for (EntityVO entity : enList) {
            Ztree enTree = new Ztree();
            enTree.setId(entity.getEnId());
            enTree.setName(entity.getEnName());
            enTree.setOpen(RespCodeEnum.RESP_CODE_ENUM_false.code());
            enTree.setIsEntity(1);
            enTree.setIconSkin(TreeIconSkin.ICON_ENTITY.getDesc());
            enTree.setIsParent(entityMapper.selectCountByPEId(entity.getEnId()) > 0 ? RespCodeEnum.RESP_CODE_ENUM_true.code() : RespCodeEnum.RESP_CODE_ENUM_false.code());
            if (StringUtils.isNotEmpty(entity.getParentOrgId()) && StringUtils.isNotEmpty(entity.getParentEnId())) {
                enTree.setpId(entity.getParentEnId());
            } else if (StringUtils.isNotEmpty(entity.getParentOrgId()) && StringUtils.isEmpty(entity.getParentEnId())) {
                enTree.setpId(entity.getParentOrgId());
            }
            tree.add(enTree);
        }
        return tree;
    }

    /**
     * 获取实体所属中心井站的orgId
     *
     * @param enId
     * @return
     * @throws Exception
     */
    public String getPOId(String enId) throws Exception {
        EntityVO entity = entityMapper.selectByEnId(enId);
        String orgId = entity.getParentOrgId();
        while (RespCodeEnum.RESP_CODE_ENUM_true.code()) {
            if (!StringUtils.isBlank(orgId)) break;
            entity = entityMapper.selectByEnId(entity.getParentEnId());
            orgId = entity.getParentOrgId();
        }
        return orgId;
    }

    /**
     * 得到某个生产实体类型的特性值
     *
     * @param entityTypeId
     * @return
     */
    @Override
    public List<EntityTypeProperty> getFeaturesByEntityTypeId(String entityTypeId) {
        return entityTypePropertyMapper.outQueryList(entityTypeId);
    }

    /**
     * 得到实体对应的特性
     *
     * @param entityId
     * @return
     */
    @Override
    public List<EntityPro> getEntityProByEntityId(String entityId) {
        return entityProMapper.getEntityProByEntityId(entityId);
    }

    /**
     * 分页查询某个组织下的实体
     *
     * @param entityVO
     * @return
     */
    @Override
    public Map<String, Object> findByPage(EntityVO entityVO) {
        List<String> orgIds = Lists.newArrayList();
        Organization organization = orgMapper.selectOrgByOrgId(entityVO.getParentOrgId());
        if (organization != null) {
            List<Organization> children = orgMapper.selectOrgByOrgPId(organization.getOrgId());
            if (children == null || children.size() <= 0) {
                orgIds.add(organization.getOrgId());
            } else {
                getChidrenOrgIds(children, orgIds);
            }
        }
        entityVO.setOrgIds(orgIds);
        Map<String, Object> result = Maps.newHashMap();
        result.put("rows", entityMapper.findByPage(entityVO));
        result.put("total", entityMapper.getPageCount(entityVO));
        return result;
    }

    /**
     * 批量删除实体
     *
     * @param entityIds
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int delBatch(List<String> entityIds) throws Exception {
        return entityMapper.delBatch(entityIds);
    }

    /**
     * 获取生产实体特性值树，根据组织id
     *
     * @param orgId
     * @return
     * @throws Exception
     */
    @Override
    public List<EntityVO> getEntityPorperties(String orgId) {
        return entityMapper.getEntityPorperties(orgId);
    }


    /**
     * 得到所有的子孩子(组织)
     *
     * @param children
     * @param retList
     */
    private void getChidrenOrgIds(List<Organization> children, List<String> retList) {
        for (Organization organization : children) {
            List<Organization> chil = orgMapper.selectOrgByOrgPId(organization.getOrgId());
            if (chil == null || chil.size() <= 0) {
                retList.add(organization.getOrgId());
            } else {
                getChidrenOrgIds(chil, retList);
            }
        }
    }


    /**
     * 对参数进行判空
     *
     * @param entity
     * @return
     */
    private Map<String, Object> isBlank(Entity entity) {
        Map<String, Object> map = new HashMap<>();
        map.put("state", RespCodeEnum.RESP_CODE_ENUM_false.code());
        if (StringUtils.isBlank(entity.getEnName())) {
            map.put("msg", "生产实体名称不能为空！");
            return map;
        }
        if (StringUtils.isBlank(entity.getEnTypeId())) {
            map.put("msg", "生产生产实体类型不能为空！");
            return map;
        }
        map.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
        return map;
    }

    /**
     * @param employee 生产实体、组织混合树
     * @param isOrg
     * @param orgId    组织机构Id  @return
     * @param keyword
     * @throws Exception
     */
    @Override
    public List<Ztree> getEntityOrgZtreeByEmployee(Employee employee, String isOrg, String orgId, String keyword) throws Exception {
        if (employee == null) {
            return null;
        }

        List<Ztree> ztreeList = new ArrayList<>();

        if (StringUtils.isEmpty(keyword)) {
            //orgId为空：初始化加载
            if (StringUtils.isEmpty(orgId)) {
                if (StringUtils.isNotEmpty(employee.getOrgId())) {
                    Organization organization = organizationMapper.selectOrgByOrgId(employee.getOrgId());

                    //获得根组织下的生产实体
                    List<Ztree> entities = this.getEntityTreeById("1", employee.getOrgId());
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);

                    //查询根组织的儿子组织
                    List<Organization> organizationList = organizationService.queryNodeByOrgId(employee.getOrgId());
                    organizationList.add(organization);

                    //根组织和儿子组织一起机构转ztree元素
                    List<Ztree> orgZtreeList = organizationService.setZtree(organizationList);
                    //重新设置isParent
                    for (Ztree ztree : orgZtreeList) {
                        setOrgZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(orgZtreeList);
                }
            //orgId不为为空：异步加载
            } else {
                //加载当前组织下的儿子组织和儿子生产实体
                if ("1".equals(isOrg)) {
                    //异步加载当前组织的儿子组织：父查子
                    List<Organization> organizationList = orgMapper.selectOrgByOrgPId(orgId);

                    //组织机构转ztree元素：isParent均为true，可以异步加载
                    List<Ztree> orgZtreeList = organizationService.setZtree(organizationList);
                    //重新设置isParent
                    for (Ztree ztree : orgZtreeList) {
                        setOrgZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(orgZtreeList);

                    //加载当前组织下的生产实体
                    List<Ztree> entities = this.getEntityTreeById("1", orgId);
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);
                } else {
                    //加载当前实体下的所有儿子实体
                    List<Ztree> entities = this.getEntityTreeById(isOrg, orgId);
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);
                }
            }
        } else {
            //orgId为空：加载目标搜索节点的树
            if (StringUtils.isEmpty(orgId)) {
                List<Ztree> totalZtreeList = this.getEntityOrgMixedZtreeByOrgId(employee.getOrgId());

                outZtreeService.removeDuplicate(totalZtreeList);

                if (totalZtreeList != null && totalZtreeList.size() > 0) {
                    //查询包含搜索字符的节点：并加入结果树
                    List<Ztree> targetNodeList = new ArrayList<>();
                    for (Ztree ztree : totalZtreeList) {
                        if (ztree.getName() != null) {
                            if (ztree.getName().toLowerCase().contains(keyword.toLowerCase())) {
                                targetNodeList.add(ztree);
                            }
                        }
                    }
                    ztreeList.addAll(targetNodeList);

                    //查询包含搜索字符的节点的祖先节点：并加入结果树
                    for (Ztree ztree : targetNodeList) {
                        List tempZtreeList = new ArrayList<>();
                        //递归查找祖先节点：不包括自身
                        outZtreeService.getAncestorNodeList(ztree, totalZtreeList, tempZtreeList);
                        ztreeList.addAll(tempZtreeList);
                    }

                    //必须去重：因为不同的目标节点可以有共通的父节点
                    outZtreeService.removeDuplicate(ztreeList);
                }
            //orgId不为空：异步加载儿子组织和儿子实体
            } else {
                if ("1".equals(isOrg)) {
                    //加载当前组织的儿子组织：父查子
                    List<Organization> organizationList = orgMapper.selectOrgByOrgPId(orgId);

                    //组织机构转ztree元素：isParent均为true，可以异步加载
                    List<Ztree> orgZtreeList = organizationService.setZtree(organizationList);
                    //重新设置isParent
                    for (Ztree ztree : orgZtreeList) {
                        setOrgZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(orgZtreeList);

                    //加载当前组织下的儿子实体
                    List<Ztree> entities = this.getEntityTreeById("1", orgId);
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);
                } else {
                    //加载当前实体下的儿子实体
                    List<Ztree> entities = this.getEntityTreeById(isOrg, orgId);
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);
                }
            }
        }

        return ztreeList;
    }

    /**
     * 获取指定组织下的所有后代组织、后代生产实体的混合树
     *
     * @param orgId
     * @return
     */
    @Override
    public List<Ztree> getEntityOrgMixedZtreeByOrgId(String orgId) throws Exception {
        List<Ztree> ztreeList = new ArrayList<>();
        if (StringUtils.isNotEmpty(orgId)) {
            //树查询自身及后代节点：向下遍历
            List<Organization> orgList = organizationService.selectDownByTreeWithoutPipeline(orgId);
            List<Ztree> orgZtreeList = organizationService.setZtree(orgList);
            //重新设置isParent
            for (Ztree orgZt : orgZtreeList) {
                setOrgZtreeIsParent(orgZt, orgZt.getId());
            }
            ztreeList.addAll(orgZtreeList);

            for (Organization org : orgList) {
                //获得根组织下的所有后代生产实体
                List<Ztree> entities = this.getEntityTreeById("1", org.getOrgId());
                //重新设置isParent
                for (Ztree ztree : entities) {
                    setEntityZtreeIsParent(ztree, ztree.getId());
                }
                ztreeList.addAll(entities);
            }

            return ztreeList;
        } else {
            return null;
        }
    }

    /**
     * 生产实体、组织、部门混合树
     *
     * @param employee 人员信息组合
     * @param isOrg    是否组织
     * @param orgId    组织机构Id
     * @param keyword  搜索值
     */
    @Override
    public List<Ztree> getEntityOrgDeptZtreeByEmployee(Employee employee, String isOrg, String orgId, String keyword) throws Exception {
        if (employee == null) {
            return null;
        }

        List<Ztree> ztreeList = new ArrayList<>();

        if (StringUtils.isEmpty(keyword)) {
            //orgId为空：初始化加载
            if (StringUtils.isEmpty(orgId)) {
                if (StringUtils.isNotEmpty(employee.getOrgId())) {
                    Organization organization = organizationMapper.selectOrgByOrgId(employee.getOrgId());

                    //获得根组织下的生产实体
                    List<Ztree> entities = this.getEntityTreeById("1", employee.getOrgId());
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);

                    //查询根组织的儿子组织
                    List<Organization> organizationList = organizationService.queryNodeByOrgId(employee.getOrgId());
                    organizationList.add(organization);

                    //根组织和儿子组织一起机构转ztree元素
                    List<Ztree> orgZtreeList = organizationService.setZtree(organizationList);
                    //重新设置isParent
                    for (Ztree ztree : orgZtreeList) {
                        setOrgZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(orgZtreeList);

                    //查询当前组织下的部门
                    List<Ztree> depZtrelList = departmentService.getDeptZtreeByOrgId(employee.getOrgId());
                    //重新设置isParent
                    if (depZtrelList != null) {
                        for (Ztree depZt : depZtrelList) {
                            //组织下的部门加入人员树
                            setDepZtreeIsParent(depZt, depZt.getId());
                            ztreeList.add(depZt);
                        }
                    }

                    //查询当前组织下的区域
                    List<RegionalMaintenance> regionalMaintenanceList = regionalMaintenanceService.ajaxLoadRegionalMaintenanceList(employee.getOrgId());
                    if(regionalMaintenanceList != null && regionalMaintenanceList.size() > 0 ){
                        for(RegionalMaintenance maintenance : regionalMaintenanceList){
                            Ztree ztree = new Ztree();
                            ztree.setId(maintenance.getRegionId());
                            ztree.setName(maintenance.getRegionName());
                            ztree.setOpen(true);
                            ztree.setIsParent(false);
                            ztree.setpId(maintenance.getOrgId());
                            ztree.setIconSkin(TreeIconSkin.ICON_DEVICE_PROCESS_AREA.getDesc());
                            ztreeList.add(ztree);
                        }
                    }
                }
            //orgId不为为空：异步加载
            } else {
                //加载当前组织下的儿子组织和儿子生产实体
                if ("1".equals(isOrg)) {
                    //异步加载当前组织的儿子组织：父查子
                    List<Organization> organizationList = orgMapper.selectOrgByOrgPId(orgId);

                    //组织机构转ztree元素：isParent均为true，可以异步加载
                    List<Ztree> orgZtreeList = organizationService.setZtree(organizationList);
                    //重新设置isParent
                    for (Ztree ztree : orgZtreeList) {
                        setOrgZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(orgZtreeList);

                    //加载当前组织下的生产实体
                    List<Ztree> entities = this.getEntityTreeById("1", orgId);
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);

                    //查询当前组织下的儿子部门
                    List<Ztree> depZtreelList = departmentService.getDeptZtreeByOrgId(orgId);
                    //重新设置isParent
                    if (depZtreelList != null) {
                        for (Ztree depZt : depZtreelList) {
                            //组织下的部门加入人员树
                            setDepZtreeIsParent(depZt, depZt.getId());
                            ztreeList.add(depZt);
                        }
                    }

                    //查询当前组织下的区域
                    List<RegionalMaintenance> regionalMaintenanceList = regionalMaintenanceService.ajaxLoadRegionalMaintenanceList(orgId);
                    if(regionalMaintenanceList != null && regionalMaintenanceList.size() > 0 ){
                        for(RegionalMaintenance maintenance : regionalMaintenanceList){
                            Ztree ztree = new Ztree();
                            ztree.setId(maintenance.getRegionId());
                            ztree.setName(maintenance.getRegionName());
                            ztree.setOpen(true);
                            ztree.setIsParent(false);
                            ztree.setpId(maintenance.getOrgId());
                            ztree.setIconSkin(TreeIconSkin.ICON_DEVICE_PROCESS_AREA.getDesc());
                            ztreeList.add(ztree);
                        }
                    }
                } else {
                    //加载当前实体下的所有儿子实体
                    List<Ztree> entities = this.getEntityTreeById(isOrg, orgId);
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);

                    //查询当前部门下的儿子部门
                    List<Ztree> depZtrelList = departmentService.queryDeptTreeByPDId(orgId);
                    if (depZtrelList != null) {
                        for (Ztree depZt : depZtrelList) {
                            //部门下的部门加入人员树
                            setDepZtreeIsParent(depZt, depZt.getId());
                            ztreeList.add(depZt);
                        }
                    }
                }
            }
        } else {
            //orgId为空：加载目标搜索节点的树
            if (StringUtils.isEmpty(orgId)) {
                List<Ztree> totalZtreeList = this.getEntityOrgDeptMixedZtreeByOrgId(employee.getOrgId());

                outZtreeService.removeDuplicate(totalZtreeList);

                if (totalZtreeList != null && totalZtreeList.size() > 0) {
                    //查询包含搜索字符的节点：并加入结果树
                    List<Ztree> targetNodeList = new ArrayList<>();
                    for (Ztree ztree : totalZtreeList) {
                        if (ztree.getName() != null) {
                            if (ztree.getName().toLowerCase().contains(keyword.toLowerCase())) {
                                targetNodeList.add(ztree);
                            }
                        }
                    }
                    ztreeList.addAll(targetNodeList);

                    //查询包含搜索字符的节点的祖先节点：并加入结果树
                    for (Ztree ztree : targetNodeList) {
                        List tempZtreeList = new ArrayList<>();
                        //递归查找祖先节点：不包括自身
                        outZtreeService.getAncestorNodeList(ztree, totalZtreeList, tempZtreeList);
                        ztreeList.addAll(tempZtreeList);
                    }

                    //必须去重：因为不同的目标节点可以有共通的父节点
                    outZtreeService.removeDuplicate(ztreeList);
                }
            //orgId不为空：异步加载儿子组织和儿子实体
            } else {
                if ("1".equals(isOrg)) {
                    //加载当前组织的儿子组织：父查子
                    List<Organization> organizationList = orgMapper.selectOrgByOrgPId(orgId);

                    //组织机构转ztree元素：isParent均为true，可以异步加载
                    List<Ztree> orgZtreeList = organizationService.setZtree(organizationList);
                    //重新设置isParent
                    for (Ztree ztree : orgZtreeList) {
                        setOrgZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(orgZtreeList);

                    //加载当前组织下的儿子实体
                    List<Ztree> entities = this.getEntityTreeById("1", orgId);
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);

                    //查询当前组织下的儿子部门
                    List<Ztree> depZtreelList = departmentService.getDeptZtreeByOrgId(orgId);
                    if (depZtreelList != null) {
                        for (Ztree depZt : depZtreelList) {
                            //组织下的部门加入树
                            setDepZtreeIsParent(depZt, depZt.getId());
                            ztreeList.add(depZt);
                        }
                    }

                    //查询当前组织下的区域
                    List<RegionalMaintenance> regionalMaintenanceList = regionalMaintenanceService.ajaxLoadRegionalMaintenanceList(orgId);
                    if(regionalMaintenanceList != null && regionalMaintenanceList.size() > 0 ){
                        for(RegionalMaintenance maintenance : regionalMaintenanceList){
                            Ztree ztree = new Ztree();
                            ztree.setId(maintenance.getRegionId());
                            ztree.setName(maintenance.getRegionName());
                            ztree.setOpen(true);
                            ztree.setIsParent(false);
                            ztree.setpId(maintenance.getOrgId());
                            ztree.setIconSkin(TreeIconSkin.ICON_DEVICE_PROCESS_AREA.getDesc());
                            ztreeList.add(ztree);
                        }
                    }
                } else {
                    //加载当前实体下的儿子实体
                    List<Ztree> entities = this.getEntityTreeById(isOrg, orgId);
                    //重新设置isParent
                    for (Ztree ztree : entities) {
                        setEntityZtreeIsParent(ztree, ztree.getId());
                    }
                    ztreeList.addAll(entities);

                    //查询当前部门下的儿子部门
                    List<Ztree> depZtrelList = departmentService.queryDeptTreeByPDId(orgId);
                    if (depZtrelList != null) {
                        for (Ztree depZt : depZtrelList) {
                            //部门下的部门加入人员树
                            setDepZtreeIsParent(depZt, depZt.getId());
                            ztreeList.add(depZt);
                        }
                    }
                }
            }
        }

        return ztreeList;
    }

    /**
     * 获取指定组织及其下面的所有后代组织、后代生产实体、后代部门的混合树
     *
     * @param orgId
     * @return
     */
    public List<Ztree> getEntityOrgDeptMixedZtreeByOrgId(String orgId) throws Exception {
        List<Ztree> ztreeList = new ArrayList<>();
        if (StringUtils.isNotEmpty(orgId)) {
            //树查询自身及后代节点：向下遍历
            List<Organization> orgList = organizationService.selectDownByTreeWithoutPipeline(orgId);
            List<Ztree> orgZtreeList = organizationService.setZtree(orgList);
            //重新设置isParent
            for (Ztree orgZt : orgZtreeList) {
                setOrgZtreeIsParent(orgZt, orgZt.getId());
            }
            ztreeList.addAll(orgZtreeList);

            for (Organization org : orgList) {
                //获得根组织下的所有后代生产实体
                List<Ztree> entities = this.getEntityTreeById("1", org.getOrgId());
                //重新设置isParent
                for (Ztree ztree : entities) {
                    setEntityZtreeIsParent(ztree, ztree.getId());
                }
                ztreeList.addAll(entities);
            }

            for(Organization org : orgList){
                //所有组织下的后代部门加入组织部门树
                List<Ztree> depZtreelList = departmentService.getDeptZtreeByOrgId(org.getOrgId());
                if (depZtreelList != null) {
                    for (Ztree depZt : depZtreelList) {
                        //组织下的部门加入人员树
                        setDepZtreeIsParent(depZt, depZt.getId());
                        ztreeList.add(depZt);
                    }
                }
            }

            for(Organization org : orgList){
                //所有组织下的区域加入实体组织部门树
                List<RegionalMaintenance> regionalMaintenanceList = regionalMaintenanceService.ajaxLoadRegionalMaintenanceList(org.getOrgId());
                if(regionalMaintenanceList != null && regionalMaintenanceList.size() > 0 ){
                    for(RegionalMaintenance maintenance : regionalMaintenanceList){
                        Ztree ztree = new Ztree();
                        ztree.setId(maintenance.getRegionId());
                        ztree.setName(maintenance.getRegionName());
                        ztree.setOpen(true);
                        ztree.setIsParent(false);
                        ztree.setpId(maintenance.getOrgId());
                        ztree.setIconSkin(TreeIconSkin.ICON_DEVICE_PROCESS_AREA.getDesc());
                        ztreeList.add(ztree);
                    }
                }
            }

            return ztreeList;
        } else {
            return null;
        }
    }

    /**
     * 只获取组织下的实体或则实体下的实体，不获取组织下的组织
     *
     * @param isOrg 1是组织 id 是组织Id
     * @param id
     * @return
     * @throws Exception
     */
    public List<Ztree> getEntityTreeById(String isOrg, String id) throws Exception {
        List<Ztree> ztList = new ArrayList<>();
        List<Organization> orgList = new ArrayList<>();

        //如果当前是实体，按上级实体ID，查询下级实体
        if (StringUtils.isNotEmpty(isOrg) && !"1".equals(isOrg) && StringUtils.isNotEmpty(id)) {
            ztList.addAll(getEntityZtreeList(entityMapper.selectByPEId(id)));
            //如果当前是组织，按所属组织ID查询该组织下的实体并转换为Ztree
        } else if (StringUtils.isNotEmpty(isOrg) && "1".equals(isOrg) && StringUtils.isNotEmpty(id)) {
            ztList.addAll(getEntityZtreeList(entityMapper.selectByPOId(id)));
        }
        return ztList;
    }

    /**
     * 组织和生产实体混合树混合树的设置组织的ztree的isParent
     *
     * @param ztree
     * @param orgId
     * @throws Exception
     */
    public void setOrgZtreeIsParent(Ztree ztree, String orgId) throws Exception {
        //查询当前组织的所有儿子组织
        List<Organization> orgList = organizationService.queryNodeByOrgId(orgId);
        //查询当前组织下的生产实体
        List<EntityVO> entityVOList = entityMapper.selectByPOId(orgId);
        if ((orgList != null && orgList.size() > 0) || (entityVOList != null && entityVOList.size() > 0)) {
            ztree.setIsParent(true);
        } else {
            ztree.setIsParent(false);
        }
    }

    /**
     * 组织和部门混合树混合树的设置部门的ztree的isParent
     * @param ztree
     * @param depId
     * @throws Exception
     */
    @Override
    public void setDepZtreeIsParent(Ztree ztree, String depId) throws Exception {
        List<DepartmentVO> sonDepList = departmentMapper.selectByPDId(depId);

        if((sonDepList != null && sonDepList.size()>0)){
            ztree.setIsParent(true);
        }else{
            ztree.setIsParent(false);
        }
    }

    /**
     * 组织和生产实体混合树混合树的设置生产实体的ztree的isParent
     *
     * @param ztree
     * @param entityId
     * @throws Exception
     */
    @Override
    public void setEntityZtreeIsParent(Ztree ztree, String entityId) throws Exception {
        //查询当前生产实体下的儿子生产实体
        List<EntityVO> entityVOList = entityMapper.selectByPEId(entityId);
        if (entityVOList != null && entityVOList.size() > 0) {
            ztree.setIsParent(true);
        } else {
            ztree.setIsParent(false);
        }
    }

    /**
     * @param nodeId   实体Id , 组织机构Id {除开井站 或者 分公司 或者 其他组织机构}
     * @param iconSkin 图标问题
     */
    @Override
    public List<Ztree> getEntityTypeByEntity(String nodeId, String iconSkin) throws Exception {
        List<Ztree> ztreeLists = new ArrayList<>();
        Organization organization = organizationService.selectOrgByOrgId(nodeId);
        //  判断组织 .  分公司 、ICON_FLAG 、气矿、作业区、井、站 、管道
        if (iconSkin.equals(TreeIconSkin.ICON_BUIDING.getDesc()) || iconSkin.equals(TreeIconSkin.ICON_FLAG.getDesc()) ||
                iconSkin.equals(TreeIconSkin.ICON_OA.getDesc()) || iconSkin.equals(TreeIconSkin.IONC_PIPELINE.getDesc())) {
            // 获取全部的实体类型
            List<Ztree> ztreeList = serviceNodeService.getServieNodeZtree();
            ztreeLists.addAll(ztreeList);
            List<Ztree> etList = entityTypeService.getAllEntityTypeZtree();
            ztreeLists.addAll(etList);
        } else if (iconSkin.equals(TreeIconSkin.ICON_STATION.getDesc()) ||
                iconSkin.equals(TreeIconSkin.IONC_WELL.getDesc())) {
            // 获取绑定的场站绑定的业务体系中的生产单元
            Station station = stationService.selectByPrimaryKey(nodeId);
            String entityTypeId = null;
            if (station != null && station.getServiceNodeId() != null) {
                entityTypeId = station.getServiceNodeId();
            }
            Well well = wellService.selectByPrimaryKey(nodeId);
            if (well != null && well.getServiceNodeId() != null) {
                entityTypeId = well.getServiceNodeId();
            }
            List<Ztree> list = entityTypeService.getEntityTypeList(entityTypeId);
            ztreeLists.addAll(list);
        } else {
            // 实体ID , 判断当前是实体
            Entity entity = entityMapper.selectByEnId(nodeId);
            if (entity != null && entity.getEnTypeId() != null) {
                EntityType entityType = new EntityType();
                entityType.setEntityTypeId(entity.getEnTypeId());
                List<Ztree> list = entityTypeService.getEntityTypeList(entity.getEnTypeId());
                ztreeLists.addAll(list);
            }

        }
        return ztreeLists;
    }

    /**
     * 得到数据单元的特性值
     *
     * @param entityPro
     * @return
     */
    @Override
    public Map<String, Object> getEntityProByDataItemId(EntityPro entityPro) {
        Map<String, Object> result = Maps.newHashMap();
        Integer count = entityTypePropertyMapper.queryEntityProOfDataItemCount(entityPro);
        List<EntityPro> entityPros = entityTypePropertyMapper.queryEntityProOfDataItemList(entityPro);
     /*   List<EntityPro> entityPros = entityProMapper.queryEntityProOfDataItemList(entityPro);
        Integer count = entityProMapper.queryEntityProOfDataItemCount(entityPro);*/
        result.put("total", count);
        result.put("rows", entityPros);
        return result;
    }

    /**
     * 解除绑定特性的汇报点
     *
     * @param devicePropertyId
     * @return
     */
    @Transactional(readOnly = false)
    public Map<String, Object> unBindDataItem(String devicePropertyId) throws Exception {
        Map<String, Object> result = Maps.newHashMap();
        try {
            entityPropertyMapMapper.unBindDataItem(devicePropertyId);
            result.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            result.put("msg", "解绑成功");
            return result;
        } catch (Exception e) {
            throw new RuntimeException("解除绑定汇报点出错");
        }
    }

    /**
     * 删除实体特性
     *
     * @param devicePropertyId
     * @return
     * @throws Exception
     */
    @Transactional(readOnly = false)
    public Map<String, Object> delEnProperty(String devicePropertyId) throws Exception {
        Map<String, Object> result = Maps.newHashMap();
        try {
            entityProMapper.delEnProperty(devicePropertyId);
            result.put("state", RespCodeEnum.RESP_CODE_ENUM_true.code());
            result.put("msg", BackMessage.MODIFY_OK);
            return result;
        } catch (Exception e) {
            throw new RuntimeException("删除特性失败");
        }
    }

    /**
     * 得到还没有被场站或者井站绑定的实体特性
     *
     * @param entityPro
     * @return
     */
    @Override
    public Map<String, Object> getEntityTypePropertyWithoutOrgBind(EntityPro entityPro) {
        Map<String, Object> result = Maps.newHashMap();
        List<EntityTypeProperty> entityTypePropertyWithoutOrgBindList = entityTypePropertyMapper.getEntityTypePropertyWithoutOrgBindList(entityPro);
        int entityTypePropertyWithoutOrgBindCount = entityTypePropertyMapper.getEntityTypePropertyWithoutOrgBindCount(entityPro);
        result.put("rows", entityTypePropertyWithoutOrgBindList == null ? Lists.newArrayList() : entityTypePropertyWithoutOrgBindList);
        result.put("total", entityTypePropertyWithoutOrgBindCount);
        return result;
    }

    /**
     * 根据设备ID，设备类型ID，组织ID查询集合
     *
     * @param record
     * @return
     */
    @Override
    public Map<String, Object> selectByList(EntityPro record) throws Exception {
        Map<String, Object> result = Maps.newHashMap();
        if (StringUtils.isEmpty(record.getEntityTypeId()) || StringUtils.isEmpty(record.getDeviceId()) || StringUtils.isEmpty(record.getEntityId())) {
            return result;
        }
        try {
            List<EntityPro> entityPros = entityProMapper.selectByList(record);
            result.put("rows", entityPros);
            result.put("total", entityPros.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}
